package com.itaming.lycheeframework.support.cipher.asymmetric;

import com.itaming.lycheeframework.support.cipher.CipherConstant;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA加密器
 *
 * @author A.Ming
 */
public class RsaEncryptor extends AbstractAsymmetricEncryptor {

    /**
     * 获取KeyFactory实例
     *
     * @return KeyFactory实例
     */
    @Override
    protected KeyFactory getKeyFactory() throws NoSuchAlgorithmException {
        return KeyFactory.getInstance(CipherConstant.ALGORITHM_RSA);
    }

    /**
     * 获取公钥KeySpec
     *
     * @param publicKey 公钥
     * @return 公钥KeySpec
     */
    @Override
    protected KeySpec getPublicKeySpec(byte[] publicKey) {
        return new X509EncodedKeySpec(publicKey);
    }

    /**
     * 获取私钥KeySpec
     *
     * @param privateKey 私钥
     * @return 私钥KeySpec
     */
    @Override
    protected KeySpec getPrivateKeySpec(byte[] privateKey) {
        return new PKCS8EncodedKeySpec(privateKey);
    }

}
